package com.mz.ozy.bean;

import android.text.TextUtils;
import android.widget.ImageView;
import android.widget.TextView;

import com.mz.ozy.R;
import com.mz.ozy.utils.CalendarUtils;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class FirmCartBean {

    private String date;
    private int totalNum = 0;
    private double totalPrice = 0.00;
    private double totalMarketPrice = 0.00;
    private double totalDiscountPrice = 0.00;
    private double market_price = 0.00;
    private int producttype;//1欧花  0普通

    public double getMarket_price() {
        return market_price;
    }

    public void setMarket_price(double market_price) {
        this.market_price = market_price;
    }

    public int getProducttype() {
        return producttype;
    }

    public void setProducttype(int producttype) {
        this.producttype = producttype;
    }

    private ImageView mCartImageView;
    private List<TextView> mTextViews = new ArrayList<>();
    private List<ImageView> mImageViews = new ArrayList<>();
    private List<CartListBean> carList = new ArrayList<>();

    public String getCheckIds() {
        StringBuilder goodsList = new StringBuilder();
        for (int i = 0; i < carList.size(); i++) {
            final CartListBean cartListBean = carList.get(i);
            if (cartListBean.check()) {
                goodsList.append(cartListBean.getId()).append(",");
            }
        }
        return goodsList.toString();
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    public List<CartListBean> getCarList() {
        return carList;
    }

    public void setItemCheck(int pos) {
        final CartListBean cartListBean = carList.get(pos);
        final ImageView imageView = mImageViews.get(pos);
        if (cartListBean.isCheck()) {
            imageView.setImageResource(R.mipmap.cb_red_select);
            totalNum = totalNum + cartListBean.getNum();
            double curPrice = 0.00;
            double curMarketPrice = 0.00;
            if (!TextUtils.isEmpty(cartListBean.getPrice())) {
                curPrice = Double.parseDouble(cartListBean.getPrice());
            }
            if (!TextUtils.isEmpty(cartListBean.getMarket_price())) {
                curMarketPrice = Double.parseDouble(cartListBean.getMarket_price());
            }
            final double curTotalPrice = curPrice * cartListBean.getNum();
            totalPrice = totalPrice + curPrice * cartListBean.getNum();
            totalMarketPrice = totalMarketPrice + curMarketPrice * cartListBean.getNum();
            if (cartListBean.getAcType() == 0) {
                final double acAmt = Double.parseDouble(cartListBean.getAcAmt());
                if (curTotalPrice >= acAmt) {
                    totalDiscountPrice = totalDiscountPrice + Double.parseDouble(cartListBean.getAcDiscountAmt());
                }
            } else if (cartListBean.getAcType() == 1) {
                final int times = (int) (curTotalPrice / Double.parseDouble(cartListBean.getAcAmt()));
                totalDiscountPrice = totalDiscountPrice + Double.parseDouble(cartListBean.getAcDiscountAmt()) * times;
            }
        } else {
            imageView.setImageResource(R.mipmap.cb_red_normal);
            totalNum = totalNum - cartListBean.getNum();
            double curPrice = 0.00;
            double curMarketPrice = 0.00;
            if (!TextUtils.isEmpty(cartListBean.getPrice())) {
                curPrice = Double.parseDouble(cartListBean.getPrice());
            }
            if (!TextUtils.isEmpty(cartListBean.getMarket_price())) {
                curMarketPrice = Double.parseDouble(cartListBean.getMarket_price());
            }
            final double curTotalPrice = curPrice * cartListBean.getNum();
            totalPrice = totalPrice - curPrice * cartListBean.getNum();
            totalMarketPrice = totalMarketPrice - curMarketPrice * cartListBean.getNum();
            if (cartListBean.getAcType() == 0) {
                final double acAmt = Double.parseDouble(cartListBean.getAcAmt());
                if (curTotalPrice >= acAmt) {
                    totalDiscountPrice = totalDiscountPrice - Double.parseDouble(cartListBean.getAcDiscountAmt());
                }
            } else if (cartListBean.getAcType() == 1) {
                final int times = (int) (curTotalPrice / Double.parseDouble(cartListBean.getAcAmt()));
                totalDiscountPrice = totalDiscountPrice - Double.parseDouble(cartListBean.getAcDiscountAmt()) * times;
            }
        }
    }

    public void addSelectNum(int pos) {
        final TextView textView = mTextViews.get(pos);
        final CartListBean cartListBean = carList.get(pos);
        final int preNum = cartListBean.getNum();
        cartListBean.setNum(cartListBean.getNum() + 1);
        textView.setText(String.valueOf(cartListBean.getNum()));
        if (cartListBean.isCheck()) {
            double curPrice = 0.00;
            double curMarketPrice = 0.00;
            if (!TextUtils.isEmpty(cartListBean.getPrice())) {
                curPrice = Double.parseDouble(cartListBean.getPrice());
            }
            if (!TextUtils.isEmpty(cartListBean.getMarket_price())) {
                curMarketPrice = Double.parseDouble(cartListBean.getMarket_price());
            }
            totalNum = totalNum + 1;
            totalPrice = totalPrice + curPrice;
            totalMarketPrice = totalMarketPrice + curMarketPrice;
            final double preTotalPrice = curPrice * preNum;
            if (cartListBean.getAcType() == 0) {
                final double acAmt = Double.parseDouble(cartListBean.getAcAmt());
                if (preTotalPrice >= acAmt) {
                    totalDiscountPrice = totalDiscountPrice - Double.parseDouble(cartListBean.getAcDiscountAmt());
                }
            } else if (cartListBean.getAcType() == 1) {
                final int times = (int) (preTotalPrice / Double.parseDouble(cartListBean.getAcAmt()));
                totalDiscountPrice = totalDiscountPrice - Double.parseDouble(cartListBean.getAcDiscountAmt()) * times;
            }

            final double curTotalPrice = curPrice * cartListBean.getNum();
            if (cartListBean.getAcType() == 0) {
                final double acAmt = Double.parseDouble(cartListBean.getAcAmt());
                if (curTotalPrice >= acAmt) {
                    totalDiscountPrice = totalDiscountPrice + Double.parseDouble(cartListBean.getAcDiscountAmt());
                }
            } else if (cartListBean.getAcType() == 1) {
                final int times = (int) (curTotalPrice / Double.parseDouble(cartListBean.getAcAmt()));
                totalDiscountPrice = totalDiscountPrice + Double.parseDouble(cartListBean.getAcDiscountAmt()) * times;
            }
        }
    }

    public void minusSelectNum(int pos) {
        final TextView textView = mTextViews.get(pos);
        final CartListBean cartListBean = carList.get(pos);
        final int preNum = cartListBean.getNum();
        cartListBean.setNum(cartListBean.getNum() - 1);
        textView.setText(String.valueOf(cartListBean.getNum()));

        if (cartListBean.isCheck()) {
            double curPrice = 0.00;
            double curMarketPrice = 0.00;
            if (!TextUtils.isEmpty(cartListBean.getPrice())) {
                curPrice = Double.parseDouble(cartListBean.getPrice());
            }
            if (!TextUtils.isEmpty(cartListBean.getMarket_price())) {
                curMarketPrice = Double.parseDouble(cartListBean.getMarket_price());
            }
            totalNum = totalNum - 1;
            totalPrice = totalPrice - curPrice;
            totalMarketPrice = totalMarketPrice - curMarketPrice;
            final double preTotalPrice = curPrice * preNum;
            if (cartListBean.getAcType() == 0) {
                final double acAmt = Double.parseDouble(cartListBean.getAcAmt());
                if (preTotalPrice >= acAmt) {
                    totalDiscountPrice = totalDiscountPrice - Double.parseDouble(cartListBean.getAcDiscountAmt());
                }
            } else if (cartListBean.getAcType() == 1) {
                final int times = (int) (preTotalPrice / Double.parseDouble(cartListBean.getAcAmt()));
                totalDiscountPrice = totalDiscountPrice - Double.parseDouble(cartListBean.getAcDiscountAmt()) * times;
            }

            final double curTotalPrice = curPrice * cartListBean.getNum();
            if (cartListBean.getAcType() == 0) {
                final double acAmt = Double.parseDouble(cartListBean.getAcAmt());
                if (curTotalPrice >= acAmt) {
                    totalDiscountPrice = totalDiscountPrice + Double.parseDouble(cartListBean.getAcDiscountAmt());
                }
            } else if (cartListBean.getAcType() == 1) {
                final int times = (int) (curTotalPrice / Double.parseDouble(cartListBean.getAcAmt()));
                totalDiscountPrice = totalDiscountPrice + Double.parseDouble(cartListBean.getAcDiscountAmt()) * times;
            }
        }
    }

    public double getDiscountPrice(int index, int num) {
        double curDiscountPrice = 0.00;
        final CartListBean cartListBean = carList.get(index);
        double curPrice = 0.00;
        if (!TextUtils.isEmpty(cartListBean.getPrice())) {
            curPrice = Double.parseDouble(cartListBean.getPrice());
        }
        final double curTotalPrice = curPrice * num;
        if (cartListBean.getAcType() == 0) {
            curDiscountPrice = Double.parseDouble(cartListBean.getAcDiscountAmt());
        } else if (cartListBean.getAcType() == 1) {
            final double times = curTotalPrice / Double.parseDouble(cartListBean.getAcAmt());
            curDiscountPrice = Double.parseDouble(cartListBean.getAcDiscountAmt()) * times;
        }
        return curDiscountPrice;
    }

    public double getMarketPrice(int index) {
        double curMarketPrice = 0.00;
        final CartListBean cartListBean = carList.get(index);
        if (!TextUtils.isEmpty(cartListBean.getMarket_price())) {
            curMarketPrice = Double.parseDouble(cartListBean.getMarket_price());
        }
        return curMarketPrice;
    }

    public double getPrice(int index) {
        double curPrice = 0.00;
        final CartListBean cartListBean = carList.get(index);
        if (!TextUtils.isEmpty(cartListBean.getPrice())) {
            curPrice = Double.parseDouble(cartListBean.getPrice());
        }
        return curPrice;
    }

    public void deleteGoodsView() {
        List<TextView> mNewTextViews = new ArrayList<>();
        List<ImageView> mNewImageViews = new ArrayList<>();
        List<CartListBean> mNewCarList = new ArrayList<>();
        for (int i = 0; i < carList.size(); i++) {
            final CartListBean cartListBean = carList.get(i);
            if (!cartListBean.check()) {
                mNewTextViews.add(mTextViews.get(i));
                mNewImageViews.add(mImageViews.get(i));
                mNewCarList.add(carList.get(i));
            }
        }
        carList = mNewCarList;
        mTextViews = mNewTextViews;
        mImageViews = mNewImageViews;
    }

    public void addTextViews(TextView textView) {
        mTextViews.add(textView);
    }

    public TextView getTextView(int pos) {
        return mTextViews.get(pos);
    }

    public void removeTextViews(int pos) {
        mTextViews.remove(pos);
    }

    public void addImageViews(ImageView imageView) {
        mImageViews.add(imageView);
    }

    public void setCurImageView(ImageView ivSelect) {
        mCartImageView = ivSelect;
    }

    public void getImageView(int pos) {
        mImageViews.get(pos);
    }

    public void removeImageViews(int pos) {
        mImageViews.remove(pos);
    }

    public void setCarList(List<CartListBean> carList) {
        this.carList = carList;
    }

    public void addProduct(CartListBean productMapBean) {
        this.carList.add(productMapBean);
    }

    public boolean setAllCheckState() {
        boolean isCheck = true;
        for (CartListBean bean : carList) {
            if (!bean.isCheck()) {
                isCheck = false;
                break;
            }
        }
        isAllCheck = isCheck;
        if (isAllCheck) {
            mCartImageView.setImageResource(R.mipmap.cb_red_select);
        } else {
            mCartImageView.setImageResource(R.mipmap.cb_red_normal);
        }
        return isAllCheck;
    }

    public boolean isItemCheck(int pos) {
        final CartListBean cartListBean = carList.get(pos);
        return cartListBean.check();
    }

    public int getChildCount() {
        return carList.size();
    }

    public int getTotalNum() {
        return totalNum;
    }

    public double getTotalPrice() {
        return totalPrice;
    }

    public double getTotalMarketPrice() {
        return totalMarketPrice;
    }

    public double getTotalDiscountPrice() {
        return totalDiscountPrice;
    }

    private boolean isAllCheck = false;

    public void setCarCheck() {
        if (isAllCheck) {
            setAllCheck(false);
        } else {
            setAllCheck(true);
        }
    }

    public void setAllCheck(boolean check) {
        isAllCheck = check;
        if (isAllCheck) {
            mCartImageView.setImageResource(R.mipmap.cb_red_select);
        } else {
            mCartImageView.setImageResource(R.mipmap.cb_red_normal);
        }
        for (int i = 0; i < carList.size(); i++) {
            final CartListBean cartBean = carList.get(i);
            if (check == false) {
                cartBean.setCheck(false);
                final ImageView imageView = mImageViews.get(i);
                imageView.setImageResource(R.mipmap.cb_red_normal);
                totalNum = 0;
                totalPrice = 0.00;
                totalMarketPrice = 0.00;
                totalDiscountPrice = 0.00;
            } else {
                if (cartBean.isCheck() != true) {
                    cartBean.setCheck(true);
                    final ImageView imageView = mImageViews.get(i);
                    imageView.setImageResource(R.mipmap.cb_red_select);
                    double curPrice = 0.00;
                    double curMarketPrice = 0.00;
                    if (!TextUtils.isEmpty(cartBean.getPrice())) {
                        curPrice = Double.parseDouble(cartBean.getPrice());
                    }
                    if (!TextUtils.isEmpty(cartBean.getMarket_price())) {
                        curMarketPrice = Double.parseDouble(cartBean.getMarket_price());
                    }
                    totalNum = totalNum + cartBean.getNum();
                    totalPrice = totalPrice + curPrice * cartBean.getNum();
                    totalMarketPrice = totalMarketPrice + curMarketPrice * cartBean.getNum();
                    final double curTotalPrice = curPrice * cartBean.getNum();
                    if (cartBean.getAcType() == 0) {
                        final double acAmt = Double.parseDouble(cartBean.getAcAmt());
                        if (curTotalPrice >= acAmt) {
                            totalDiscountPrice = totalDiscountPrice + Double.parseDouble(cartBean.getAcDiscountAmt());
                        }
                    } else if (cartBean.getAcType() == 1) {
                        final int times = (int) (curTotalPrice / Double.parseDouble(cartBean.getAcAmt()));
                        totalDiscountPrice = totalDiscountPrice + Double.parseDouble(cartBean.getAcDiscountAmt()) * times;
                    }
                }
            }
        }
    }

    public String getFormatDate() {
        Date curDate = null;
        try {
            curDate = CalendarUtils.string2Date(date);
        } catch (ParseException e) {
        }
        if (curDate == null) {
            return null;
        } else {
            return CalendarUtils.date2String(curDate);
        }
    }
}
